﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLFacade.entities;
using BLFacade;

namespace ProjectUI
{
    public partial class ProjectListForm : Form
    {
        private List<IProject> projects;
        private ICustomer customer;
        public ProjectListForm()
        {
            InitializeComponent();
            gridViewProjects.AutoGenerateColumns = false;
            projects = FacadeHolder.DataFacade.GetProjects();
            gridViewProjects.DataSource = projects;
            this.Text = "All projects";
            fillChart();
        }

        public ProjectListForm(ICustomer customer)
        {
            InitializeComponent();
            gridViewProjects.AutoGenerateColumns = false;
            this.customer = customer;
            projects = customer.Projects;
            gridViewProjects.DataSource = projects;
            if (FacadeHolder.UserFacade.IsAdministrator()) btnAdd.Visible = true;
            this.Text = String.Format("Projects of {0}", customer.Name);
            fillChart();
        }

        public ProjectListForm(IEmployee manager)
        {
            InitializeComponent();
            gridViewProjects.AutoGenerateColumns = false;
            projects = manager.MyProjects;
            gridViewProjects.DataSource = projects;
            this.Text = String.Format("Managed projects of {0}", manager.FullName);
            fillChart();
        }

        private void gridViewProjects_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = gridViewProjects.SelectedCells[0].RowIndex;
            ProjectForm info = new ProjectForm(((List<IProject>)gridViewProjects.DataSource)[index]);
            info.MdiParent = this.MdiParent;
            info.Show();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ProjectForm info = new ProjectForm(customer);
            info.MdiParent = this.MdiParent;
            info.Show();
            this.Close();
        }

        private void fillChart()
        {
            string[] statuses = {"Created", "In progress", "Completed", "Overdue"};
            
            chrtProjects.Series[0].Points.AddXY("Created", 
                projects.Where(t => !t.StartDate.HasValue).Count());
            chrtProjects.Series[0].Points.AddXY("In progress",
                projects.Where(t => t.StartDate.HasValue && !t.EndDate.HasValue && t.DeadLine.CompareTo(DateTime.Now) >= 0).Count());
            chrtProjects.Series[0].Points.AddXY("Completed",
                projects.Where(t => t.StartDate.HasValue && t.EndDate.HasValue).Count());
            chrtProjects.Series[0].Points.AddXY("Overdue",
                projects.Where(t => t.StartDate.HasValue && !t.EndDate.HasValue && t.DeadLine.CompareTo(DateTime.Now) < 0).Count());
            

            foreach (var item in chrtProjects.Series[0].Points)
            {
                item["PieLabelStyle"] = "Disabled";   
            }
            
        }

        //public void UpdateDataSource()
        //{
        //    if (customer != null) gridViewProjects.DataSource = customer.Projects;
        //}

    }
}
